package top.hkyzf.study.leetcode.string;

import org.junit.Assert;
import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-22 15:43
 */
public class 反转字符串中的单词III_557 {
    public String reverseWords(String s) {
        String[] subStrs = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <subStrs.length ; i++) {
            StringBuilder t = new StringBuilder(subStrs[i]);
            sb.append(t.reverse());
            if(i != subStrs.length-1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    public String reverseWords1(String s) {
        StringBuilder sb = new StringBuilder(s);
        String string = sb.reverse().toString();
        String[] strs = string.split(" ");
        StringBuilder str = new StringBuilder();
        for (int i = strs.length-1; i >= 0; i--) {
            str.append(strs[i]);
            if (i != 0) {
                str.append(" ");
            }
        }
        return str.toString();
    }

    @Test
    public void testReverseWords () {
        String s = "Let's take LeetCode contest";
        System.out.println(reverseWords1(s));
        Assert.assertEquals("s'teL ekat edoCteeL tsetnoc", reverseWords1(s));
    }
}
